//
//  BaseMenuController.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/7/29.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit

class BaseMenuController: BaseViewController {
    
    lazy var titleButtons : [UIButton] = [UIButton]()
    
    var selectButton : UIButton?
    
    var seletIndex = 0
    
    lazy var titleScrollView : UIScrollView = {
        
        let temp = UIScrollView(frame: CGRect(x: 0, y: ZMNavMaxY, width: KScreenW, height: ZMTitlesViewH))
        
        temp.showsHorizontalScrollIndicator = false
        
        temp.backgroundColor = UIColor.white
        
        return temp
        
    }()
    
    
    lazy var contentScrollView : UIScrollView = {
        
        let temp = UIScrollView(frame: CGRect(x: 0, y: 0, width: KScreenW, height: KScreenH))
        
        temp.backgroundColor = UIColor.white
        
        temp.isPagingEnabled = true
        
        temp.bounces = false
        
        temp.showsHorizontalScrollIndicator = false
        
        return temp
        
    }()
    
    var isInitial  = false
    
    var underline : UIView?
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(contentScrollView)
        
         contentScrollView.delegate = self

        view.addSubview(titleScrollView)
        
        automaticallyAdjustsScrollViewInsets = false
        
        QL1(self.view.bounds)

        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        
        super.viewWillAppear(animated)
        
        if isInitial == false {
            isInitial = true
            
            setupAllTitle()
        }
        
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

   
}



// MARK:- setupSubviews
extension BaseMenuController{
    
    
    func setupAllTitle() {
        
        let count = childViewControllers.count
        
        var x : CGFloat = 0
        let y : CGFloat = 0
        var w : CGFloat = 0
        
        if count > 5 {
            
            w = KScreenW / 5

        } else {
            
            w = KScreenW / CGFloat(count)

        }
        
        let h : CGFloat = ZMTitlesViewH
        
        for (key,value) in childViewControllers.enumerated() {
            
            x = CGFloat(key) * w
            let btn = LightDisableBtn(type: .custom)
            btn.tag = key
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            btn.frame = CGRect(x: x, y: y, width: w, height: h)
            btn.setTitle(value.title, for: .normal)
            btn.setTitleColor(UIColor.black, for: .normal)
            btn.addTarget(self, action: #selector(titleDidClick(btn:)), for: .touchUpInside)
            
            titleScrollView.addSubview(btn)
            
            titleButtons.append(btn)
            
            
        }
        
        
        let btn = titleButtons[seletIndex]
        
        
        
        titleScrollView.contentSize = CGSize(width: CGFloat(count) * w, height: 0)
        
        contentScrollView.contentSize = CGSize(width: CGFloat(count) * KScreenW, height: 0)
        
        titleDidClick(btn: btn)
        
        setupLine()
        
        setupBtnUnderline()
    }
    
    
    func setupLine(){
        
        let line = UIView(frame: CGRect(x: 0, y: ZMTitlesViewH - 0.5, width: KScreenW, height: 0.5))
        line.backgroundColor = UIColor.lineGray()
        titleScrollView.addSubview(line)
        
    }
    
    
    
    func setupBtnUnderline(){
        let btn = titleScrollView.subviews[seletIndex] as! LightDisableBtn
        
        let lineView = UIView(frame: CGRect(x: 0, y: ZMTitlesViewH - 2, width: 0, height: 2))
        
        lineView.backgroundColor = btn.titleColor(for: .selected)
        
        titleScrollView.addSubview(lineView)
        
        underline = lineView
        
        btn.isSelected = true
        
        //        btn.titleLabel?.sizeToFit()
        
        underline?.frame.size.width = KScreenW / CGFloat(childViewControllers.count)
        
        underline?.center.x = btn.center.x
    }
    
    
    
    
    
    func titleDidClick(btn: UIButton){
        
        let i = btn.tag
        
        selectBtn(btn: btn)
        
        let offsetX = CGFloat(i) * KScreenW;
        
        contentScrollView.contentOffset = CGPoint(x: offsetX, y: 0)
    }
    
    
    func selectBtn(btn: UIButton){
        
        selectButton?.setTitleColor(UIColor.black, for: .normal)
        
        btn.setTitleColor(UIColor.red, for: .normal)
        
        setupTitleButtonCenter(btn: btn)
        
        selectButton?.transform = CGAffineTransform.identity
        
        btn.transform = CGAffineTransform.init(scaleX: 1.2, y: 1.2)
        
        selectButton = btn
        
        
        UIView.animate(withDuration: 0.5, animations: {
            //            self.underline?.frame.size.width = KScreenW / CGFloat(self.childViewControllers.count)
            
            self.underline?.center.x = btn.center.x
            
            
        }) { (_) in
            
            self.setupOneViewControllerView(tag: btn.tag)
            
        }
    }
    
    
    
    func setupTitleButtonCenter(btn: UIButton){
        
        var offsetX = btn.center.x - KScreenW * 0.5
        
        if offsetX < 0 {
            offsetX = 0
        }
        
        
        let  maxOffsetX = titleScrollView.contentSize.width - KScreenW
        
        if offsetX > maxOffsetX {
            offsetX = maxOffsetX
        }
        
        titleScrollView.setContentOffset(CGPoint(x: offsetX, y: 0), animated: true)
        
        
        
    }
    
    
    func setupOneViewControllerView(tag: Int){
        
        
        let x = CGFloat(tag) * KScreenW
        
        let tempV = childViewControllers[tag]
        
        if tempV.view.superview != nil {
            return
        }
        
        
        tempV.view.frame = CGRect(x: x, y: 0, width: KScreenW, height: contentScrollView.bounds.size.height)
        
        contentScrollView.addSubview(tempV.view)
        
    }
    
    
    
}






// MARK:- UIScrollViewDelegate
extension BaseMenuController{
    
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let i  : Int = Int (scrollView.contentOffset.x / KScreenW)
        
        let btn = titleButtons[i]
        
        selectBtn(btn: btn)
        
        
        setupOneViewControllerView(tag: i)
        
        
        
    }
    
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let leftI  : Int = Int (scrollView.contentOffset.x / KScreenW)
        
        let rightI = leftI + 1
        
        
        let leftBtn = titleButtons[leftI]
        
        let count = childViewControllers.count
        
        var rightBtn : UIButton? = nil
        
        if rightI < count{
            
            rightBtn = titleButtons[rightI]
        }
        
        
        var rightSale = scrollView.contentOffset.x / KScreenW
        
        
        rightSale = rightSale - CGFloat(leftI)
        
        let leftSale = 1 - rightSale
        
        var rihtBtnCenterX = rightBtn?.center.x
        
        let leftBtnCenterX = leftBtn.center.x
        
        if rightBtn == nil {
            
            rihtBtnCenterX = 0.0
        }
        
        let subtrat = fabs(rihtBtnCenterX! - leftBtnCenterX)
        
        let temp = leftBtn.center.x + rightSale * subtrat
        
        underline?.center.x = temp
        
        
        leftBtn.transform = CGAffineTransform.init(scaleX: leftSale * 0.2 + 1, y: leftSale * 0.2 + 1)
        
        rightBtn?.transform = CGAffineTransform.init(scaleX: rightSale * 0.2 + 1, y: rightSale * 0.2 + 1)
        
        
        rightBtn?.setTitleColor( UIColor(red: rightSale, green: 0, blue: 0, alpha: 1), for: .normal)
        
        leftBtn.setTitleColor( UIColor(red: leftSale, green: 0, blue: 0, alpha: 1), for: .normal)
        
    }
    
}
